package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentRepairReview;
import com.mxpio.erp.common.equipment.service.EquipmentRepairReviewService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;

@Tag(name = "EquipmentRepairReviewController", description = "设备维修-故障复盘记录")
@RestController
@RequestMapping("/erp/equipmnet/spotrepair/review/")
public class EquipmentRepairReviewController {

    @Autowired
    private EquipmentRepairReviewService reviewService;

    @GetMapping("list")
    @Operation(summary = "设备维修-故障复盘记录列表", description = "设备维修-故障复盘记录列表(无分页)", method = "GET")
    public Result<List<EquipmentRepairReview>> list(@Parameter(description = "查询参数") Criteria criteria) {
        List<EquipmentRepairReview> list = reviewService.list(EquipmentRepairReview.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "设备维修-故障复盘记录列表", description = "设备维修-故障复盘记录列表(分页)", method = "GET")
    public Result<Page<EquipmentRepairReview>> page(@Parameter(description = "查询参数") Criteria criteria, @Parameter(description = "每页条数") Integer pageSize, @Parameter(description = "页号") Integer pageNo) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo - 1, pageSize);
        Page<EquipmentRepairReview> list = reviewService.listPage(EquipmentRepairReview.class, page, criteria);
        return Result.OK(list);
    }

    @GetMapping("list/{bizNo}")
    @Operation(summary = "根据bizNo工单编码获取设备维修-故障复盘记录列表", description = "根据bizNo工单编码获取设备维修-故障复盘记录列表", method = "GET")
    public Result<List<EquipmentRepairReview>> getById(@PathVariable(name = "bizNo", required = true) String bizNo) {
        Criteria criteria = Criteria.create();
        criteria.addCriterion("bizNo", Operator.EQ, bizNo);
        List<EquipmentRepairReview> list = reviewService.list(EquipmentRepairReview.class, criteria);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存设备维修-故障复盘记录列表", description = "保存设备维修-故障复盘记录列表")
    public Result<EquipmentRepairReview> add(@RequestBody EquipmentRepairReview equipmentRepairReview) {
        reviewService.save(equipmentRepairReview);
        return Result.OK(equipmentRepairReview);
    }

}
